package com.rds.common.utils.rds;

import com.rds.common.core.service.IDynamicService;
import com.rds.common.utils.DateUtils;
import com.rds.common.utils.SecurityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 历史记录-->通过方法类
 * @version : 1.0
 * @Copyright: Copyright(c) 2020-2021 All right reserved
 * @Company: GUANGZHOU RDS Co.,LTD
 * @author: LiChuanlong
 * @create Date : 2021/12/13 : 23:04
 */
public class HistoryRecordUtil {
    private IDynamicService dynamicService;

    public HistoryRecordUtil(){}

    public HistoryRecordUtil(IDynamicService dynamicService){
        this.dynamicService = dynamicService;
    }

    /**** 修订/修改对象 这种需要记录原始值的 后面在考虑 先把历史记录功能实现*/

    /**
     * 记录历史记录(一般操作)
     * @param businessId 当前业务对象Id
     * @param action 操作类型
     * @param businessStateId 当前业务对象状态Id
     * @param modular 模块名称
     */
    public void recordHistory(Long businessId,String action,Integer businessStateId,String modular,String operateRecord){
        String operateUser = SecurityUtils.getUsername();
        Map<String, Object> data=new HashMap<>();
        data.put("business_id",businessId);
        data.put("operate_action",action);
        data.put("operate_user",operateUser);
        data.put("operate_time", DateUtils.getNowDate());
        data.put("stateid", businessStateId);
        data.put("modular", modular);
        data.put("operate_scope", 2);
        data.put("operate_record", operateRecord);
        dynamicService.insertData("sys_history",data);
    }


    /**
     *  修订时记录历史记录
     * @param businessId 当前业务对象Id
     * @param action 操作 这里为修订
     * @param businessStateId 业务对象状态
     * @param modular 模块
     * @param historyBuilder 记录信息
     */
    public void reviseHistory(Long businessId,String action,Integer businessStateId,String modular,StringBuilder historyBuilder){
        String operateUser = SecurityUtils.getUsername();
        Map<String, Object> data=new HashMap<>();
        data.put("business_id",businessId);
        data.put("operate_action",action);
        data.put("operate_user",operateUser);
        data.put("operate_time", DateUtils.getNowDate());
        data.put("stateid", businessStateId);
        data.put("modular", modular);
        data.put("operate_scope", 2);
        data.put("operate_record", historyBuilder);
        dynamicService.insertData("sys_history",data);
    }

    /**
     * 记录历史记录(生命周期提升降级)
     * @param id 当前对象Id
     * @param currentStateId 当前状态Id
     * @param targetStateId  提升/降级后状态Id
     * @param operate 提升/降级
     */
    public void lifeCycleHistory(Long id,int currentStateId,int targetStateId,String operate){
        String operateAction = "promote".equalsIgnoreCase(operate) ? "提升" : "demote".equalsIgnoreCase(operate) ? "降级" : "提升";

        LifecycleUtil lifecycleUtil = new LifecycleUtil(dynamicService);
        String oldStateName = lifecycleUtil.getStateName(currentStateId);
        String newStateName = lifecycleUtil.getStateName(targetStateId);

        String operateRecord = "状态由:"+oldStateName+"-->"+newStateName;

        Map<String, Object> data=new HashMap<>();
        data.put("business_id",id);
        data.put("operate_action",operateAction);
        data.put("operate_user",SecurityUtils.getUsername());
        data.put("operate_time", DateUtils.getNowDate());
        data.put("stateid", targetStateId);
        data.put("modular", "生命周期");
        data.put("operate_scope", 1);
        data.put("operate_record", operateRecord);
        dynamicService.insertData("sys_history",data);
    }
}
